--POUR CONTROLE


define erp='erp_mli2.';
define outils='outils_mli2.';

 

 DELETE FROM se_ART_LIGNE;


-- select * from  uv_clignes



 INSERT INTO se_ART_LIGNE
 (SELECT id ligne_cod,
         0 enreg_id,
         regroup ligneregr_cod,
         'N' enreg_s,
         libelle ligne_lib,
         NULL ligne_lib2,
         gamme_moli critlibre1_val,
         NULL critlibre2_val
    FROM uv_clignes) ;

 INSERT INTO se_ART_LIGNE
 (SELECT COMBATTVAL_VAL ligne_cod,
         0 enreg_id,
         null ligneregr_cod,
         'N' enreg_s,
         'Pour reprise article vrt=TPR2' ligne_lib,
         NULL ligne_lib2,
         null critlibre1_val,
         NULL critlibre2_val
   FROM (
            SELECT COMBATTVAL_VAL, Count(*)
            FROM temp_se_VRT_COMBSTOVAL t WHERE vrt_cod ='TPR' AND att_cod =2
            AND NOT EXISTS (SELECT 1 FROM erp_mli2.art_ligne c WHERE c.ligne_cod = t.combattval_val)
            AND t.combattval_val IS NOT NULL
            AND Length(COMBATTVAL_VAL)<=3
            GROUP BY COMBATTVAL_VAL )
         ) ;

 UPDATE se_ART_LIGNE tf SET enreg_id=(select enreg_id FROM &erp.ART_LIGNE f WHERE f.ligne_cod=tf.ligne_cod) ;
 UPDATE se_ART_LIGNE tf SET enreg_id=&outils.enreg_id.NEXTVAL WHERE enreg_id IS NULL;



 CALL uvse_synchroverserp('enreg_id','ART_LIGNE','O') ;

 commit;
 
 
select * from (
SELECT &outils.stragg(source) source,ligne_cod, enreg_id, ligneregr_cod, enreg_s, ligne_lib, ligne_lib2, critlibre1_val, critlibre2_val
FROM (
  SELECT 'uv' source ,g.* FROM se_ART_LIGNE g
  UNION ALL
  SELECT 'se' source ,g.* FROM &erp.ART_LIGNE g
  )
GROUP BY ligne_cod, enreg_id, ligneregr_cod, enreg_s, ligne_lib, ligne_lib2, critlibre1_val, critlibre2_val
HAVING Count(1)!=2) r
ORDER BY ligne_cod;